package com.platform.modules.wallet.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.exception.BaseException;
import com.platform.common.web.domain.AjaxResult;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.modules.wallet.dao.WalletRechargeCashDao;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.domain.WalletAccount;
import com.platform.modules.wallet.domain.WalletRechargeCash;
import com.platform.modules.wallet.enums.AccountTypeEnum;
import com.platform.modules.wallet.enums.RechargeCashStatusEnum;
import com.platform.modules.wallet.service.WalletAccountService;
import com.platform.modules.wallet.service.WalletKPayService;
import com.platform.modules.wallet.service.WalletRechargeCashService;
import com.platform.modules.wallet.service.WalletService;
import com.platform.modules.wallet.vo.AccountVo01;
import com.platform.modules.wallet.vo.RechargeCashVo01;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
@Service("walletRechargeCashService")
public class WalletRechargeCashServiceImpl extends BaseServiceImpl<WalletRechargeCash> implements WalletRechargeCashService {

    @Resource
    private WalletService walletService;
    @Resource
    private WalletAccountService walletAccountService;
    @Resource
    private WalletRechargeCashDao walletRechargeCashDao;
    @Resource
    private WalletKPayService walletKPayService;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(walletRechargeCashDao);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized boolean createRechargeCash(RechargeCashVo01 vo01) {

        WalletRechargeCash cash = this.create(vo01);

        AccountVo01 accountVo = new AccountVo01();
        accountVo.setOtherId(cash.getId());
        accountVo.setAmount(vo01.getAmount());
        accountVo.setUserId(vo01.getUserId());

        Boolean negative = null;

        if (vo01.getType().equals(AccountTypeEnum.RECHARGE)) {
            accountVo.setType(AccountTypeEnum.RECHARGE);
            negative = Boolean.FALSE;
        } else if (vo01.getType().equals(AccountTypeEnum.CASH)) {
            accountVo.setType(AccountTypeEnum.CASH);
            negative = WalletAccountService.NEGATIVE;
        }else {
            throw new BaseException("非存取业务");
        }

        WalletAccount account = walletAccountService.createRecord(accountVo, negative);

        Wallet wallet = walletService.getWallet(vo01.getUserId());

        if (negative){
            walletKPayService.withdraw(wallet.getWalletAddress(),
                    new BigDecimal(vo01.getAmount() / 100),
                    String.valueOf(cash.getId()),
                    vo01.getWalletPassword());
        }else {
            walletKPayService.deposit(wallet.getWalletAddress(),
                    new BigDecimal(vo01.getAmount() / 100),
                    String.valueOf(cash.getId()),
                    vo01.getWalletPassword());
        }

        wallet.setWalletPassword(vo01.getWalletPassword());
        walletService.update(wallet);

        walletAccountService.successRecord(account.getId());//完成账本记录

//        if (negative){
//            walletService.subtract(accountVo.getAmount(), account.getUserId());//减掉余额
//        }else {
//            walletService.add(accountVo.getAmount(), account.getUserId());
//        }

        successCash(cash.getId());

        return true;
    }

    @Override
    public Integer querySumAll() {
        return walletRechargeCashDao.querySumAll();
    }

    @Override
    public Integer getRechargeToday() {
        return walletRechargeCashDao.querySum(DateUtil.today(), AccountTypeEnum.RECHARGE);
    }

    @Override
    public Integer getRechargeAll() {
        return walletRechargeCashDao.querySum(null,AccountTypeEnum.RECHARGE);
    }

    @Override
    public Integer getCashToday() {
        return walletRechargeCashDao.querySum(DateUtil.today(), AccountTypeEnum.CASH);
    }

    @Override
    public Integer getCashAll() {
        return walletRechargeCashDao.querySum(null,AccountTypeEnum.CASH);
    }

    /**
     * 完成提现
     */
    private void successCash(Long id) {
        WalletRechargeCash cash = findById(id);
        cash.setStatus(RechargeCashStatusEnum.SUCCESS);
        cash.setSuccessTime(DateUtil.date());
        updateById(cash);
    }

    private WalletRechargeCash create(RechargeCashVo01 vo) {
        WalletRechargeCash bean = getBean();
        bean.setUserId(vo.getUserId());

        if (vo.getType().equals(AccountTypeEnum.RECHARGE)) {
            bean.setType(AccountTypeEnum.RECHARGE);
            bean.setAmount(vo.getAmount());
        } else if (vo.getType().equals(AccountTypeEnum.CASH)) {
            bean.setType(AccountTypeEnum.CASH);
            bean.setAmount(-vo.getAmount());
        }else {
            throw new BaseException("非存取业务");
        }

        this.add(bean);
        return bean;
    }

    private WalletRechargeCash getBean() {
        WalletRechargeCash cash = new WalletRechargeCash();
        cash.setStatus(RechargeCashStatusEnum.INIT);
        DateTime date = DateUtil.date();
        cash.setCreateDate(date);
        cash.setCreateTime(date);
        return cash;
    }

}
